package com.ruoyi.pcwl.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.pcwl.domain.CarouselItem;
import com.ruoyi.pcwl.domain.MediaFiles;
import com.ruoyi.pcwl.domain.ScenicSpot;
import com.ruoyi.pcwl.domain.query.ScenicSpotQuery;

import com.ruoyi.pcwl.domain.vo.Result;
import com.ruoyi.pcwl.domain.dto.ScenicSpotDto;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.IMediaFilesService;
import com.ruoyi.pcwl.service.ScenicSpotService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 景区信息表 Controller
 */

@Api(tags = "景区信息表控制层")
@RestController("scenicSpotController")
@RequestMapping("/scenicSpot")
public class ScenicSpotController extends BaseController{

	@Resource
	private ScenicSpotService scenicSpotService;

	@Value("${minio.bucketName}")
	private String bucketName;

	@Resource
	private IMediaFilesService mediaFilesService;
	/**
	 * 根据条件分页查询
	 */
	@ApiOperation("根据条件分页查询")
	@GetMapping("/loadDataList")
	public Result loadDataList(ScenicSpotQuery query){
		return Result.ok(scenicSpotService.selectPage(query));
	}

	/**
	 * 新增
	 */
	@ApiOperation("新增")
	@PostMapping("/add")
	public Result add(@RequestBody ScenicSpot scenicSpot) {

		//保存景区信息
		scenicSpotService.add(scenicSpot);

		return Result.ok(null);
	}

//	/**
//	 * 新增
//	 */
//	@ApiOperation("新增")
//	@PostMapping("/add")
//	public Result add(ScenicSpotDto scenicSpotDto) {
//
//		MultipartFile file = scenicSpotDto.getFile();
//		//上传文件到minio
//		Map<String, Object> map = null;
//		try {
//			map = MinioUtil.uploadFile(file, MediaTypeEnum.SCENICSPOT);
//		} catch (Exception e) {
//			throw new RuntimeException("上传文件失败");
//		}
//
//		//保存景区信息
//		ScenicSpot scenicSpot = new ScenicSpot();
//		BeanUtils.copyProperties(scenicSpotDto,scenicSpot);
//		scenicSpot.setPic(map.get("path").toString());
//		scenicSpot.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));
//
//		scenicSpotService.add(scenicSpot);
//
//		return Result.ok(null);
//	}

	/**
	 * 批量新增
	 */
	@ApiOperation("批量新增")
	@PostMapping("/addBatch")
	public Result addBatch(@RequestBody List<ScenicSpot> listBean) {
		scenicSpotService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 批量新增/修改
	 */
	@ApiOperation("批量新增/修改")
	@PostMapping("/addOrUpdateBatch")
	public Result addOrUpdateBatch(@RequestBody List<ScenicSpot> listBean) {
		scenicSpotService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@ApiOperation("根据Id查询对象")
	@GetMapping("/getScenicSpotById/{id}")
	public Result getScenicSpotById(@PathVariable Long id) {
		return Result.ok(scenicSpotService.getScenicSpotById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@ApiOperation("根据Id修改对象")
	@PutMapping("/updateScenicSpotById/{id}")
	public Result updateScenicSpotById(@RequestBody ScenicSpot bean,@PathVariable Long id) {
		scenicSpotService.updateScenicSpotById(bean);
		return Result.ok(null);
	}

	/**
	 * 根据Id删除
	 */

	@ApiOperation("根据Id删除")
	@DeleteMapping("/deleteScenicSpotById/{id}")
	public Result deleteScenicSpotById(@PathVariable Long id) {
		scenicSpotService.deleteScenicSpotById(id);
		return Result.ok(null);
	}
}
